//
//  BookingCloud.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/16/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "BookingCloud.h"
#import "BookingsManager.h"
#import "QuartzCore/QuartzCore.h"
#import "UIViewAdditions.h"
#import "Utils.h"
#import "ConfigurationManager.h"
#import "Constants.h"

#define BOOKING_CLOUD_WIDTH (IS_IPAD() ? 400 : 253)

@implementation BookingCloud

@synthesize courseTitle, teacherName, typeOfClass, studyGroups, backgroundView, booking, calendarImageView;

- (id)initWithBooking:(Booking *)aBooking {
    self = [super init];
    if (self) {
        [self setBooking:aBooking];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setUpSubViews];
    }
    return self;
}

- (void)setUpSubViews {
    [[NSBundle mainBundle] loadNibNamed:@"BookingCloud" owner:self options:nil];
    self.frame = backgroundView.frame;
    [self addSubview:backgroundView];
    self.width = BOOKING_CLOUD_WIDTH;
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;


    
    // BackgroundView
    backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"background_color"]] colorWithAlphaComponent:0.8];
    backgroundView.userInteractionEnabled = NO;
    
    // Text colors
    UIColor *normalTextColor = [Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"welcome_screen_labels_color"]];
    UIColor *highlightedTextColor = [Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"welcome_screen_labels_color"]];
    courseTitle.textColor = normalTextColor;
    courseTitle.highlightedTextColor = highlightedTextColor;
    teacherName.textColor = normalTextColor;
    teacherName.highlightedTextColor = highlightedTextColor;
    typeOfClass.textColor = normalTextColor;
    typeOfClass.highlightedTextColor = highlightedTextColor;
    studyGroups.textColor = normalTextColor;
    studyGroups.highlightedTextColor = highlightedTextColor;
    
    // Calendar Image view
    calendarImageView.image = [UIImage imageNamed:[[ConfigurationManager instance] UIConfigValueForKey:@"calendar_image"]];
    
}



- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    [courseTitle setHighlighted:highlighted];
    [teacherName setHighlighted:highlighted];
    [typeOfClass setHighlighted:highlighted];
    [studyGroups setHighlighted:highlighted];
    if (!highlighted) {
        backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"background_color"]] colorWithAlphaComponent:0.8];
    } else {
        backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
}

- (void)setBooking:(Booking *)aBooking {
    booking = aBooking;
    courseTitle.text = aBooking.course.courseName;
    typeOfClass.text = aBooking.typeOfClass.name;
    teacherName.text = [NSString stringWithFormat:@"%@ %@", booking.teacher.firstName, booking.teacher.lastName];
    studyGroups.text = [[BookingsManager instance] stringWithBookingStudyGroups:booking.studyGroups];
    
    if([booking.startDate compare:[NSDate date]] == NSOrderedAscending) {
        self.userInteractionEnabled = NO;
        backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"booking_cell_past_background"]] colorWithAlphaComponent:0.8];
    }
    // Calendar Image
    calendarImageView.hidden = ![[BookingsManager instance] bookingIsInCalendar:booking];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
